/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/12
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.Controller;

import com.it1997.it1997admin.pojo.Role;
import com.it1997.it1997admin.service.PermissionService;
import com.it1997.it1997admin.service.RoleService;
import com.it1997.it1997admin.utils.ResultUtil;
import com.it1997.it1997admin.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@Controller
@Api(tags = "角色接口")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/getAllRoles")
    @ResponseBody
    @ApiOperation(value = "获取所有角色")
    public String getAllRoles(Integer page,Integer limit) {
        Integer dataCount=roleService.getRoleCount();
        List<Role> roleList = roleService.getAllRoles(page,limit);
        if (roleList.size() != 0)
            return ResultUtil.tableData(0, "成功",dataCount, roleList);
            return ResultUtil.fail(1, "失败", roleList);
            }
    @GetMapping("/getAllRolesList")
    @ResponseBody
    @ApiOperation(value = "获取所有角色集合，放入下拉框")
    public String getAllRolesList() {
        Integer dataCount=roleService.getRoleCount();
    List<Role> roleList = roleService.getAllRolesList();
        if (roleList.size() != 0)
                return ResultUtil.tableData(0, "成功",dataCount, roleList);
        return ResultUtil.fail(1, "失败", roleList);
}
    @PostMapping("/addRole")
    @ResponseBody
    @ApiOperation(value = "添加管理员角色")
    @Transactional
    public String addRole(@RequestBody RoleVo roleVo) {
       // log.info(roleVo);
        //log.info(roleVo.getRoleName()+":::"+roleVo.getDescription()+":::"+roleVo.getPermissionList());
        int tem = roleService.addRole(roleVo);
        if (tem == 1) {
            return ResultUtil.fail(1, "添加角色成功");
        }else if(tem==2){
            return ResultUtil.fail(2,"新增角色失败，未知异常");
        }
            return ResultUtil.fail(0, "角色已存在");
    }
    @PostMapping("/updateRole")
    @ResponseBody
    @ApiOperation(value = "修改用户角色信息")
    @Transactional
    public String updateRole(@RequestBody RoleVo roleVo) {
        int i = roleService.updateRole(roleVo);
        if (i == 1) {
            return ResultUtil.fail(1, "修改角色成功");
        }
        return ResultUtil.success(0, "修改角色失败");
    }
    @PostMapping("/deleteRole")
    @ResponseBody
    @ApiOperation(value = "删除管理员角色")
    public String deleteRole(@RequestBody List<RoleVo> roleVoList){
        int i=roleService.deleteRole(roleVoList);
        if(i>0){
            return ResultUtil.success(1,"删除成功");
        }else {
            return ResultUtil.fail(0,"删除失败");
        }

    }
    @GetMapping("/getRoleById")
    @ResponseBody
    @ApiOperation(value = "根据角色id查询角色信息")
    public String getRoleById(@RequestParam("roleId") Integer roleId){
        log.info("角色id："+roleId);
       RoleVo roleVo= roleService.getRoleByRoleId(roleId);
        return ResultUtil.success(1,"查询成功",roleVo);
    }
    @PostMapping("/searchRole")
    @ResponseBody
    @ApiOperation(value = "搜索角色")
    public String searchRole(@RequestBody RoleVo roleVo) {
        Integer roleCount=roleService.getSearchRoleCount(roleVo);
        List<RoleVo> roleVoList=roleService.searchRole(roleVo);
        if(roleVoList.size()>0){
            return ResultUtil.tableData(0,"查询成功",roleCount,roleVoList);
            }else {
            return ResultUtil.fail(1,"暂无数据");
        }

    }
}
